// pages/details/details.js
const app = getApp()
const time = require('../../utils/util.js')
Page({
     data: {
          autoplay: true,
          interval: 4000,
          duration: 1000,
          id: "", //文件idz详情
          userid: "", //用户id
          imgheights: [],//所有图片的高度 
          //图片宽度 
          imgwidth: 750,
          //默认  
          current: 0 
     },
     onLoad: function(options) {
          var that = this
          that.setData({
               id: options.id,
               userid: options.userid,
          })
          that.request()
     },
     // 跳转
     navigator: function () {
          wx.switchTab({
               url: "/pages/index/index"
          })
     },
     // banner高度
     imageLoad: function (e) {//获取图片真实宽度  
          var imgwidth = e.detail.width,
               imgheight = e.detail.height,
               //宽高比  
               ratio = imgwidth / imgheight;
          // console.log(imgwidth, imgheight)
          //计算的高度值  
          var viewHeight = 750 / ratio;
          var imgheight = viewHeight;
          var imgheights = this.data.imgheights;
          //把每一张图片的对应的高度记录到数组里  
          imgheights[e.target.dataset.id] = imgheight;
          this.setData({
               imgheights: imgheights
          })
     },
     bindchange: function (e) {
          // console.log(e.detail.current)
          this.setData({ current: e.detail.current })
     },
     //     复制粘贴
     coces: function (e) {
          var that = this;
          var data = e.currentTarget.dataset.name
          wx.setClipboardData({
               data: data,
               success: function (res) {
                    wx.getClipboardData({
                         success: function (res) {
                              // console.log(res)
                         }
                    })
               }
          })
     },
     request: function() {
          var that = this
          var pages = that.data.pages;
          var id = that.data.id;
          var userid = that.data.userid;
          var types = that.data.types
          wx.showLoading({
               title: '加载中...',
          })
          wx.request({
               url: app.data.apiurl + '/ddk/detail/' + id,
               data: {
                    token: userid
               },
               header: {
                    'content-type': 'application/json' // 默认值
               },
               method: 'GET',
               dataType: 'json',
               success: function(res) {
                    wx.hideLoading()
                    var list = res.data.data.goods_detail
                    var app = res.data.data.we_app_info.page_path
                    var appid = res.data.data.we_app_info.app_id
                    var arry = {}
                    arry.desc = list.goods_desc;
                    arry.name = list.goods_name;
                    arry.urls = list.goods_gallery_urls;
                    arry.quantity = list.sold_quantity;
                    if (((list.min_normal_price / 100).toString()).indexOf(".") == "-1") {
                         arry.group = (list.min_group_price- list.coupon_discount) / 100;
                         arry.normal = list.min_group_price / 100;
                         arry.discount = list.coupon_discount / 100;
                    }else{
                         var nun = (list.min_normal_price/ 100).toString().split(".")[1].length
                         arry.group = ((list.min_group_price - list.coupon_discount) / 100).toFixed(nun);
                         arry.normal = list.min_group_price / 100;
                         arry.discount = list.coupon_discount / 100;
                    }
                    
                    arry.start_time = time.formatTime(new Date(list.coupon_start_time * 1000));
                    arry.end_time = time.formatTime(new Date(list.coupon_end_time * 1000));
                    arry.total = list.coupon_total_quantity;
                    arry.remain = list.coupon_remain_quantity;
                    arry.app = list.we_app_web_view_short_url;
                    that.setData({
                         body: arry,
                         app: app,
                         appid: appid
                    })
               },
               fail: function(res) {
                    wx.showToast({
                         title: '网络异常',
                         icon: 'none',
                         duration: 2000
                    })
               }
          })
     },
     onShareAppMessage: function (res) {
          var that = this
          if (res.from === 'button') {
               // 来自页面内转发按钮
               //console.log(res.target)
          }
          return {
               title: "拼多多内部优惠券",
               path: '/pages/details/details?id=' + that.data.id + '&userid=' + wx.getStorageSync("userid"),
               success: function (res) {
                    //console.log(res)
                    wx.showToast({
                         title: '发送成功',
                         icon: 'none'
                    })
               },
               fail: function (res) {
                    // 转发失败
                    wx.showToast({
                         title: '发送失败',
                         icon: 'none'
                    })
               }
          }

     }
})